package com.show.web;


import com.show.dto.sys_emp.SysEmp;
import com.show.filter.LoginController;
import com.show.filter.ValidateCode;
import com.show.service.emp.EmpService;
import com.show.service.event.EventService;
import com.show.service.sys_menu.MenuService;
import com.show.util.HelperClazz;
import com.show.util.Md5Salt;
import com.show.web.event.EventController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主方法
 * @Auther : 白瑜
 * @Param : 
 * @date : 2018-01-25 上午 9:44
 */
@Controller
//@RequestMapping("/main")
public class mainController {

    @Autowired
    EmpService empService;
    @Autowired
    private Md5Salt md5Salt;
    @Autowired
    private MenuService menuService;
    @Autowired
    private LoginController loginController;
    @Autowired
    private EventService eventService;

    @Autowired
    private EventController eventController;
    @Autowired


    @GetMapping("/header")
    @ApiOperation(value = "跳转RoleInsert", notes = "")
    public String header(Map<String, Object> map) throws Exception {
        return "header";
    }

    /**
     * 响应验证码页面
     * @return
     */
    @RequestMapping(value="/validateCode")
    public String validateCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(80,30,4,100);
        session.setAttribute("code", vCode.getCode());
        vCode.write(response.getOutputStream());
        return null;
    }


    /**
     * 获取个人资料信息
     * @param emp
     * @return map
     * baiyu
     */
    @PostMapping(value = "/getUserInfo")
    @ResponseBody
    public Object getUserInfo(SysEmp emp) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SysEmp> list = empService.getUserInfo(emp.getLoginName());//获取当前登陆人信息及角色信息
        List<SysEmp> list1 = empService.getUserOrgName(emp.getLoginName());//获取当前登陆人所属部门
        map.put("sysEmp",list.get(0));
        map.put("orgName",list1.get(0));
        return map;
    }

    /**
     * 修改密码
     * @param emp
     * baiyu
     */
    @PostMapping(value = "/modifyPwd")
    @ResponseBody
    public int modifyPwd(SysEmp emp,HttpServletRequest request){
        int flag = 0;
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'","");//获取当前登陆人
        List<SysEmp> sysEmpList = empService.getPwd(userName);
        String oldPwd = sysEmpList.get(0).getLoginPwd();
        String pwd = md5Salt.getMD5(emp.getLoginPwd()); //盐值加密
        if(oldPwd.equals(pwd)){
            if(emp.getNewPass().equals(emp.getNewPass1())){
                String newPwd = md5Salt.getMD5(emp.getNewPass());
                int i = empService.updatePwd(newPwd,userName);
                if(i ==1){
                    flag = 3;   //密码修改成功
                }else {
                    flag = 4;   //密码修改失败
                }
            }else {
                flag = 2;       //2次输入密码不一致
            }
        }else {
            flag = 1;          //原始密码不对
        }
        return flag;
    }


}
